
/*
Copyright 2009 Peter Ekler, Akos Ludanyi, Budapest University of Technology and Economics, Department of Automation and Applied Informatics, Applied Mobile Research Group (AMORG) http://amorg.aut.bme.hu

This file is part of MobTorrent.

MobTorrent is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

MobTorrent is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with MobTorrent.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ui;

import data.LocalizationSupport;
import data.MTPreferences;
import data.MTRMSManager;
import javax.microedition.io.ConnectionNotFoundException;
import javax.microedition.midlet.*;

/**
 * @author Tyrael
 */
public class MobTorrentMidlet extends MIDlet
{
//-- Variables
    private MTUIManager uiManager;
//--

//-- Functions

    public void initialize()
    {
        // set default localization
        //LocalizationSupport.initLocalizationSupport();
        // set settings from RMS
        try {
            MTRMSManager.restoreSettingsFromRMS();
        } catch (Exception ex) {
            LocalizationSupport.initLocalizationSupport();
            ex.printStackTrace();
        }

        uiManager = new MTUIManager(this);
        uiManager.showScreen(MTListTorrents.ScreenID);
        uiManager.synchronizeAddressBooster();
        
        try {
            MTRMSManager.restoreTorrentsFromRMS(uiManager);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

//--

// Refresh application

    public void executeUpdate(int aSelectedMirror)
    {
        try {
            switch (aSelectedMirror)
            {
                case 0:
                    platformRequest(MTPreferences.MobTorrentDownloadMIRROR1);
                    break;
                case 1:
                    platformRequest(MTPreferences.MobTorrentDownloadMIRROR2);
                    break;
                default:
                    platformRequest(MTPreferences.MobTorrentDownloadMIRROR1);
                    break;
            }
            
        } catch (ConnectionNotFoundException ex) {
            uiManager.showMessage(
                LocalizationSupport.getMessage("text_updateFailed"));
        }
    }

// --

//-- Life cycle

    public void startApp() {
        initialize();
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional)
    {
        if (uiManager!=null)
        {
            uiManager.endTorrentManager();
        }

        try {
            MTRMSManager.storeSettingsToRMS();
            MTRMSManager.storeTorrentsToRMS(uiManager.getTorrentManager());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
//--
}

